package com.wusuowei.shiro_jwt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wusuowei.shiro_jwt.annotation.CrudLog;
import com.wusuowei.shiro_jwt.common.enums.BusinessType;
import com.wusuowei.shiro_jwt.model.po.SysLog;
import com.wusuowei.shiro_jwt.service.SysLogService;
import com.wusuowei.shiro_jwt.utils.R;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

/**
 * @author yangyouwang
 * @title: SysLogController
 * @projectName crud
 * @description: 日志控制层
 * @date 2021/4/111:06 AM
 */

@RestController
@RequestMapping("syslog")

public class SysLogController  {


    @Autowired
    private  SysLogService sysLogService;




    /**
     * 列表请求
     *
     * @return 请求列表
     */


//    @RequiresRoles(value = {"admin","vip"},logical = Logical.OR)//满足一个角色就能访问
    @GetMapping("/page")
    public Map<String, Object> findPage(
                      @RequestParam Integer pageNum,
                      @RequestParam Integer pageSize,
                      @RequestParam(required = false) String title,
                      @RequestParam(required = false) String businessType) {
        Page<SysLog> page = sysLogService.findPage(new Page<>(pageNum, pageSize), title,businessType);
        List<SysLog> records = page.getRecords();
        System.out.println(records);
        page.setRecords(records);
        return R.ok().setData(page);
    }

    /**
     * 删除单个条目
     * @param id
     * @return
     */
//    @CrudLog(title = "删除日志",businessType = BusinessType.DELETE)
    @RequiresRoles(value = {"admin","vip"},logical = Logical.OR)//满足一个角色就能访问
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        sysLogService.removeById(id);

        return R.ok();
    }
    /**
     * 删除多个条目
     */
//    @CrudLog(title = "删除日志",businessType = BusinessType.DELETE)
    @RequiresRoles(value = {"admin","vip"},logical = Logical.OR)//满足一个角色就能访问
    @PostMapping("/del/batch")
    public R deleteBatch(@RequestBody List<Integer> ids) {
        sysLogService.removeBatchByIds(ids);

        return R.ok();
    }

    @RequiresRoles(value = {"admin","vip"},logical = Logical.OR)//满足一个角色就能访问
    @GetMapping("/{id}")
    public R findOne(@PathVariable Integer id) {
        return R.ok().setData(sysLogService.getById(id));
    }
}
